package com.ittao.taoaicodemother.manage.storage.factory;

import com.ittao.taoaicodemother.config.AppStorageConfig;
import com.ittao.taoaicodemother.manage.storage.enums.StorageType;
import com.ittao.taoaicodemother.manage.storage.strategy.FileStorageStrategy;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件存储策略工厂类
 * 根据配置自动选择合适的存储策略
 */
@Slf4j
@Component
public class FileStorageStrategyFactory {

    @Resource
    private AppStorageConfig storageConfig;

    @Resource
    private List<FileStorageStrategy> storageStrategies;

    private final Map<StorageType, FileStorageStrategy> strategyMap = new HashMap<>();

    @PostConstruct
    public void init() {
        // 初始化策略映射
        for (FileStorageStrategy strategy : storageStrategies) {
            strategyMap.put(strategy.getStorageType(), strategy);
            log.info("注册文件存储策略: {}", strategy.getStorageType().getDescription());
        }

        log.info("当前配置的存储类型: {}", storageConfig.getType());
        log.info("当前使用的存储策略: {}", getCurrentStrategy().getStorageType().getDescription());
    }

    /**
     * 获取当前配置的存储策略
     */
    public FileStorageStrategy getCurrentStrategy() {
        StorageType type = StorageType.fromCode(storageConfig.getType());
        FileStorageStrategy strategy = strategyMap.get(type);

        if (strategy == null) {
            log.warn("未找到存储策略: {}，使用默认本地存储策略", type);
            strategy = strategyMap.get(StorageType.LOCAL);
        }

        return strategy;
    }


}